<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BROWSER CACHE</title>
    <style>
        #result {
            width: 200px;
            height: 100px;
            border: solid 1px #90b
        }
    </style>
</head>

<body>
    <button>点击发送</button>
    <button>点击取消</button>
    <div id="result"></div>
</body>
<script lang="javascript">
    const BASE_URL = "http://localhost:8000"
    // get button element
    const send_btn = document.getElementsByTagName("button")[0];
    const cancel_btn = document.getElementsByTagName("button")[1];
    const result = document.getElementById("result");
    let xhr = null;
    // sending flag
    let isSending = false;
    // bind action to element
    send_btn.addEventListener("click", () => {
        if(isSending) xhr.abort();
        xhr = new XMLHttpRequest();
        // alter flag
        isSending = true;
        xhr.open('get', BASE_URL + '/timeout', true);
        xhr.send()
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4){
                isSending = false;
                if(xhr.status >=200 && xhr.status < 300){
                    result.innerHTML = xhr.response;
                }
            }
        }
    })
    cancel_btn.onclick = ()=>{
        xhr.abort();
    }
</script>

</html>